unit AfterDownload_XML;

interface

uses
 JclStreams,
 JclSimpleXML;

const
 XMLName = 'AfterDownload.xml';

procedure LoadSettings(AFilename: String; XML: TJclSimpleXML);
procedure SaveSettings(AFilename: String; XML: TJclSimpleXML);

var
 SetXML: TJclSimpleXML = nil;

implementation

uses
 SysUtils,
 AfterDownload_Defines;

const
 sSilent = 'Silent';
 sRule = 'Rule';
 sCondition = 'Condition';
 sAction = 'Action';
 sEnabled = 'Enabled';
 sNextRule = 'NextRule';
 sName = 'Name';
 sType = 'Type';
 sDestination = 'Destination';
 sParameter = 'Parameter';
 sFlag = 'Flag';
 sExisting = 'Existing';

procedure LoadSettings(AFilename: String; XML: TJclSimpleXML);
 var
  el, elrule: TJclSimpleXMLElem;
  i, t, ir, ri, ic, ci: Integer;
begin
 (*
 if Assigned(XML) and FileExists(AFilename) then
  begin
   for i:=High(ADRules) downto 0 do
    begin
     SetLength(ADRules[i].Actions, 0);
     SetLength(ADRules[i].Conditions, 0);
    end;
   SetLength(ADRules, 0);
   //
   XML.FileName:=AFilename;
   //load common settins
   Silent:=XML.Root.Items.BoolValue(sSilent);
   //load rules
   i:=0;
   repeat
    //get rule item
    ir:=XML.Root.Items.IndexOf(sRule+IntToStr(i));
    if ir>=0 then
     begin
      elrule:=XML.Root.Items[ir];
      if Assigned(elrule) then
       begin
        SetLength(ADRules, Length(ADRules)+1);
        ri:=High(ADRules);
        //read rule properties
        ADRules[ri].Enabled:=elrule.Properties.BoolValue(sEnabled, false);
        ADRules[ri].NextRule:=elrule.Properties.BoolValue(sNextRule, false);
        ADRules[ri].Name:=elrule.Properties.Value(sName);
        //read conditions
        t:=0;
        repeat
         //get condition item
         ic:=elrule.Items.IndexOf(sCondition+IntToStr(t));
         if ic>=0 then
          begin
           el:=elrule.Items[ic];
           if Assigned(el) then
            begin
             SetLength(ADRules[ri].Conditions, Length(ADRules[ri].Conditions)+1);
             ci:=High(ADRules[ri].Conditions);
             //read condition
             ADRules[ri].Conditions[ci].CondParam:=el.Value;
             ADRules[ri].Conditions[ci].CondType:=el.Properties.IntValue(sType);
            end;
          end;
         inc(t);
        until ic<0;
        //read actions
        t:=0;
        repeat
         //get action item
         ic:=elrule.Items.IndexOf(sAction+IntToStr(t));
         if ic>=0 then
          begin
           el:=elrule.Items[ic];
           if Assigned(el) then
            begin
             SetLength(ADRules[ri].Actions, Length(ADRules[ri].Actions)+1);
             ci:=High(ADRules[ri].Actions);
             //read action
             ADRules[ri].Actions[ci].Enabled:=el.Items.BoolValue(sEnabled, false);
             ADRules[ri].Actions[ci].ActType:=el.Items.IntValue(sType);
             ADRules[ri].Actions[ci].Dest:=el.Items.Value(sDestination);
             ADRules[ri].Actions[ci].Param:=el.Items.Value(sParameter);
             ADRules[ri].Actions[ci].Flag:=el.Items.BoolValue(sFlag, false);
            end;
          end;
         inc(t);
        until ic<0;
       end;
     end;
    Inc(i);
   until ir<0;
   XML.Free;
  end;
 *)
end;

procedure SaveSettings(AFilename: String; XML: TJclSimpleXML);
 var
  el, elrule: TJclSimpleXMLElem;
  i, t: Integer;
begin
 (*
 if not Assigned(XML) then
   Exit;
 //setup XML header
 XML.Prolog.Encoding:='Windows-1251';
 XML.Root.Name:='Settings';
 XML.Root.Clear;
 //save common settings
 XML.Root.Items.Add(sSilent, Silent);
 //save rules
 for i:=0 to High(ADRules) do
  begin
   //create rule
   elrule:=XML.Root.Items.Add(sRule+IntToStr(i));
   //set properties
   elrule.Properties.Add(sName, ADRules[i].Name);
   elrule.Properties.Add(sEnabled, ADRules[i].Enabled);
   elrule.Properties.Add(sNextRule, ADRules[i].NextRule);
   //create conditions
   for t:=0 to High(ADRules[i].Conditions) do
    begin
     el:=elrule.Items.Add(sCondition+IntToStr(t), ADRules[i].Conditions[t].CondParam);
     el.Properties.Add(sType, ADRules[i].Conditions[t].CondType);
    end;
   //create actions
   for t:=0 to High(ADRules[i].Actions) do
    begin
     el:=elrule.Items.Add(sAction+IntToStr(t));
     el.Items.Add(sEnabled, ADRules[i].Actions[t].Enabled);
     el.Items.Add(sType, ADRules[i].Actions[t].ActType);
     el.Items.Add(sDestination, ADRules[i].Actions[t].Dest);
     el.Items.Add(sParameter, ADRules[i].Actions[t].Param);
     el.Items.Add(sFlag, ADRules[i].Actions[t].Flag);
     el.Items.Add(sExisting, ADRules[i].Actions[t].Existing);
    end;
  end;
 XML.Options:=[sxoAutoIndent, sxoTrimPrecedingTextWhitespace]; 
 XML.SaveToFile(AFileName);
 *)
end;

end.
